package com.easybuy.web.controller.manager;

import com.easybuy.bean.EasybuyComment;
import com.easybuy.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/guestbook")

public class CommentController {
    @Autowired
    private ICommentService iCommentService;
    //后台分页显示
    @RequestMapping("/listByPage")
    public ModelAndView listByPage(@RequestParam(value="page",defaultValue="1") Integer page,
                                   @RequestParam(value = "pageSize",defaultValue ="3") Integer pageSize){
        //调用service的分页方法
        List<EasybuyComment> comments = iCommentService.listBypage(page,pageSize);
        //构造modelAndView
        ModelAndView mv = new ModelAndView();
        //数据
        mv.addObject("comments",comments);
        mv.addObject("commentPage",page);
        mv.addObject("maxPage",(int)Math.ceil(iCommentService.countAll()/(double)pageSize));

        //视图
        mv.setViewName("manage/guestbook");
        return mv;
    }
    //前台分页显示
    @RequestMapping("/listHomeByPage")
    public ModelAndView listByPage1(@RequestParam(value="page",defaultValue="1") Integer page,
                                   @RequestParam(value = "pageSize",defaultValue ="3") Integer pageSize){
        //调用service的分页方法
        List<EasybuyComment> comments = iCommentService.listHomeBypage(page,pageSize);
        //构造modelAndView
        ModelAndView mv = new ModelAndView();
        //数据
        mv.addObject("comments",comments);
        mv.addObject("commentPage",page);
        mv.addObject("maxPage",(int)Math.ceil(iCommentService.countAll()/(double)pageSize));

        //视图
        mv.setViewName("manager/guestbook");
        return mv;
    }
    //添加
    @RequestMapping("/to_save")
    public String toSave(){
        return "manager/guestbook";
    }

    @RequestMapping("/save_commit")
    public String saveCommit(EasybuyComment easybuyComment){
        //1.获取表单的值一参数  参数校验
        System.out.println(easybuyComment.getEcContent()+"+++"+easybuyComment.getEcNickName());
        if(easybuyComment.getEcContent()==null||easybuyComment.getEcContent().trim().length()==0){
            return "manager/paramError";
            //throw new EbExection();
        }
//        easybuyComment.setEcReplyTime("1");
//        easybuyComment.setEcCreateTime("1");
        //自动调用时间的方法（String类型）
        Date d1=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        easybuyComment.setEcCreateTime(sdf.format(d1));

        //2.调service保存的方法
        int result = iCommentService.save(easybuyComment);

        if (result <=0){
            //TODO 要么return 要么跑出异常
        }
        return "commonr/guestbook-right";
    }

    //留言表回复操作
    @RequestMapping("/reply")
    public String edit(Model model, Integer ecId){
        //1.根据留言的编号查询留言的详细信息
        EasybuyComment easybuyComment = iCommentService.selectByComment(ecId);
        //2.将查出来的新闻信息传递给guestbook-modify.jsp
        model.addAttribute("easybuyComment",easybuyComment);
        //System.out.println(easybuyComment);
        return "manage/guestbook-modify";
    }
    @RequestMapping("/replySubmit")
    public String editSubmit(EasybuyComment easybuyComment){
        //1.回复留言信息
        //自动调用时间的方法（String类型）
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        easybuyComment.setEcReplyTime(sdf.format(d));
        int result = iCommentService.updateById(easybuyComment);
        //2.跳转到更新结果页面
        if(result <=0){
            //TODO  要么return错误页面  要么跑出异常
        }
        return "manage/manage-result";
    }
    //留言表删除操作
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        //1.参数校验

        //2.执行删除（真实项目中  delete from 表名 where id =？）
        int result = iCommentService.deleteByPk(id);
        if (result==0){
            //TODO  要么return错误页面  要么跑出异常

        }
        return "manage/manage-result";
    }

}
